﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DataAccess;

namespace GUI
{
    public partial class frmThuePhong : Form
    {
        public frmThuePhong()
        {
            InitializeComponent();
        }
        PhieuThueDTO ptDTO = null;
        Status TrangThai = Status.Xem;

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnPhieuThueMoi.Enabled = true;
            frmKhachHang frm = new frmKhachHang();
            frm.Show();
        }

        

        //Hiển thị danh sách phòng lên DataGripView
        private void LoadDataPhong()
        {
            phongDTOBindingSource.DataSource = PhongBUS.SelectAll();
        }

        private void AnHienTxt(Boolean t)
        {
            cboMaPhong.Enabled = !t;
            dtpNgayThue.Enabled = !t;
            cboMaKH.Enabled = !t;
        }

        //Ẩn hiện Button
        private void ButtonLoad()
        {
            if (TrangThai == Status.Xem)
            {
                btnPhieuThueMoi.Enabled = false;
                btnThem.Enabled = true;
                btnLuu.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else if (TrangThai == Status.Them)
            {
                btnPhieuThueMoi.Enabled = false;
                btnLuu.Enabled = true;
            }
            else if (TrangThai == Status.Sua || TrangThai == Status.Xoa)
            {
                btnLuu.Enabled = true;
                btnPhieuThueMoi.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
        }

        //Hiển thị danh sách phiếu thuê lên DataGripView
        private void LoadDataPhieu()
        {
            phieuThueDTOBindingSource.DataSource = PhieuThueBUS.SelectAll();

            phieuThueDTODataGridView.Enabled = true;
            btnPhieuThueMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
        }
        private void frmThuePhong_Load(object sender, EventArgs e)
        {
            Status TrangThai = Status.Xem;
            LoadDataPhong();
            LoadDataPhieu();

            DataSet dsKH = SqlAccess.GetTable("KhachHang");
            cboMaKH.DataSource = dsKH.Tables[0];
            cboMaKH.DisplayMember = "TenKh";
            cboMaKH.ValueMember = "MaKh";

            DataSet dsPhong = SqlAccess.LayPhong();
            cboMaPhong.DataSource = dsPhong.Tables[0];
            cboMaPhong.DisplayMember = "TenPhong";
            cboMaPhong.ValueMember = "MaPhong";

            AnHienTxt(true);

            ButtonLoad();
        }

        private void phieuThueDTODataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btnPhieuThueMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
        }

        private void btnPhieuThueMoi_Click(object sender, EventArgs e)
        {
            ptDTO = phieuThueDTOBindingSource.AddNew() as PhieuThueDTO;
            TrangThai = Status.Them;

            frmThuePhong_Load(sender, e);
            ButtonLoad();
            AnHienTxt(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ptDTO.MaKH = cboMaKH.SelectedValue.ToString();
            ptDTO.MaPhong = cboMaPhong.SelectedValue.ToString();
            ptDTO.NgayThue = dtpNgayThue.Text;
            ptDTO.TinhTrang = "Dang thue";
            int iMaPhong = Convert.ToInt32(cboMaPhong.SelectedValue.ToString());

            string strKQ = null;
            if (TrangThai == Status.Them)
            {
                strKQ = PhieuThueBUS.Them(ptDTO);
                if (strKQ == "True")
                {
                    SqlAccess.CapNhatTrangThaiPhong(1, iMaPhong);
                    ptDTO = null;
                    TrangThai = Status.Xem;
                    LoadDataPhieu();
                    LoadDataPhong();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
            if (TrangThai == Status.Sua)
            {
                strKQ = PhieuThueBUS.CapNhat(ptDTO);
                if (strKQ == "True")
                {
                    ptDTO = null;
                    TrangThai = Status.Xem;
                    LoadDataPhieu();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }        
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            ptDTO = phieuThueDTOBindingSource.Current as PhieuThueDTO;
            TrangThai = Status.Sua;
            AnHienTxt(false);
            ButtonLoad();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string strKQ = null;
            ptDTO = phieuThueDTOBindingSource.Current as PhieuThueDTO;
            if (MessageBox.Show("Bạn có chắc xóa", "Thông tin", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                strKQ = PhieuThueBUS.Xoa(ptDTO);
                if (strKQ == "True")
                {
                    phieuThueDTOBindingSource.RemoveCurrent();

                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dlgHoiThoat;
            dlgHoiThoat = MessageBox.Show("Bạn có chắc thoát??", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dlgHoiThoat == DialogResult.Yes)
                this.Close();
        }
    }
}
